package com.CampusLife.controller;

import  com.CampusLife.bean.News;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import  com.CampusLife.service.INewsService;
import  com.CampusLife.until.DataTime;
import  com.CampusLife.until.ImagesUpload;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    @Resource
    private INewsService iNewsService;
    @RequestMapping("/queryAll")
    public  String  findall(Model model){
        List<News> news=iNewsService.queryAll();
        model.addAttribute("news",news);
        return  "admin/newbasic";
    }

    @RequestMapping(path = "/addNew", method = RequestMethod.POST)
    public String addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, ModelMap model, String title, String authority,String news_txt,  Integer viewNumber) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
            News news=new News();
          news.setAuthority(authority);
          news.setNews_txt(news_txt);
          news.setPhoto(photo);
          news.setTitle(title);
          news.setViewNumber(viewNumber);
            DataTime dataTime=new DataTime();
            news.setTime(dataTime.gettime());
            iNewsService.add(news);


        return "jsp/cc";
    }
    //跳转到修改页面
    @RequestMapping("/jumpUpdateUserEdit")
    public String jumpUpdateUserEdit(Integer nid,Model model){

        //数据回显
        News news=iNewsService.queryById(nid);
        model.addAttribute("news",news);
        return "jsp/update";
    }
    //修改·通告·    /图片上传
    @RequestMapping(path = "/updateNews", method = RequestMethod.POST)
    public String updateNews(@RequestParam("file") MultipartFile file, HttpServletRequest request,
                          HttpServletResponse response, ModelMap model, String title, String authority,String news_txt,  Integer viewNumber) throws Exception {
        ImagesUpload imagesUpload =new ImagesUpload();
        String photo=  imagesUpload.Upload(file);
            News news=new News();
            news.setAuthority(authority);
            news.setNews_txt(news_txt);
            news.setPhoto(photo);
            news.setTitle(title);
            news.setViewNumber(viewNumber);
            DataTime dataTime=new DataTime();
            news.setTime(dataTime.gettime());
            iNewsService.update(news);


        return "jsp/cc";
    }
    @RequestMapping("/deleteNews")
    public String deleteUser( Integer nid){

        iNewsService.delete(nid);
        return "redirect:/user/query";
    }

}
